package Predicate;

import java.util.function.Predicate;

/*
Predicate<T>:
    default predicate<T> and (Predicate other):返回一个组合判断，对应短路与
    default Predicate<T> or (Predicate other):返回一个组合判断，对应短路或
 */
public class PredicateDemo02 {
    public static void main(String[] args) {
        boolean b1 = checkString("hello", s -> s.length() > 8);
        System.out.println(b1);
        boolean b2 = checkString("helloworld", s -> s.length() > 8);
        System.out.println(b2);

        boolean b3=checkString("hello", s->s.length()>8,s->s.length()<15);
        System.out.println(b3);//false   b1是false,b2是true,逻辑与返回false

        boolean b4=checkString("helloworld",s -> s.length()>8,s -> s.length()<15);
        System.out.println(b4);
    }

    //同一个字符串给出两个不同的判断条件，最后把这两个判断的结果做逻辑运算的结果作为最终结果
    private static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2) {
//        boolean b1 = pre1.test(s);
//        boolean b2 = pre2.test(s);
//        boolean b = b1 && b2;//将b1和b2做逻辑与运算
//        return b;

        //default Predicate<T> and (Predicate other):返回一个组合判断，对应短路与
//        return pre1.and(pre2).test(s);//对同一个字符串操作 return (t) -> test(t) && other.test(t);

        //default Predicate<T> or (Predicate other):返回一个组合判断，对应短路或
        return pre1.or(pre2).test(s);
    }

    private static boolean checkString(String s, Predicate<String> pre) {
        return pre.test(s);
    }
}
